//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package farm;

import commands.BuyAnimal;
import commands.BuyObject;
import commands.CancelMysteryBox;
import commands.CancelShipment;
import commands.CheckMysteryBoxLocked;
import commands.ClaimAchivement;
import commands.ClaimShipment;
import commands.CollectAnimalGoods;
import commands.CollectCompletedBuildingExp;
import commands.CollectProductions;
import commands.FadeAwayMysteryBox;
import commands.FixBuilding;
import commands.GetOrder;
import commands.OpenMysteryBox;
import commands.Shipment;
import commands.SpeedUp;
import commands.SpeedUpBuildingConstruction;
import commands.SpeedUpProduction;
import commands.StartAnimalGoods;
import commands.StartProduction;
import commands.StartTutorial;
import commands.TutorialCompleted;
import common.AccountProcess;
import common.FarmsWork;
import common.GlobalID;
import common.IAccountProcess;
import common.IDs;
import common.Logger;
import common.SocksProxy;
import common.StringUtils;
import common.Utils;
import exceptions.AccountStopAndReconnectException;
import exceptions.AccountStopException;
import init.StaticData;
import location.DisplayObject;
import location.DisplayObjectTimer;
import location.DisplayProductionObject;
import location.Farm;
import location.LogicTimer;
import messages.Base;
import messages.ChangeFarmName;
import messages.EndClientTurn;
import messages.EndClientTurnOK;
import messages.GoHome;
import messages.Login;
import messages.LoginOK;
import messages.OwnCityData;
import messages.OwnFishingData;
import messages.OwnHomeData;
import messages.Unknown;
import messages.VisitedHomeData;
import objects.AnimalEntry;
import objects.Animals;
import objects.AnimalsProduction;
import objects.ConstructionObject;
import objects.ExpLevels;
import objects.FactoryEntry;
import objects.Field;
import objects.LogicEventData;
import objects.LogicEventDataEntry;
import objects.MysteryBoxState;
import objects.OrderEntry;
import objects.OrderEntry.state;
import objects.ProcessingBuildings;
import objects.ProductionFactories;
import objects.ProductionsGoodEntry;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class Worker implements IAccountProcess {
	private Login profile;
	private AccountProcess ap;
	private final Integer index;
	private final SocksProxy socks;
	private final String name;
	private Farm farm = null;
	private int lastTutorial;
	private Date lastReload = null;
	private LogicTimer nextOrderTimer = null;
	private int orderToOrderTime = 30;
	Date startTime;
	private boolean done = false;
	private boolean claimRssSlots = false;
	private boolean completeTutorials = false;
	private int completedTutorials = 0;
	boolean feedmillDone = false;
	boolean harvestDone = false;
	int iterations = 0;
	int carwait = 0;
	private int f9trycount = 0;
	private int f12trycount = 0;
	private boolean finallyCheckedMysteryBox = false;
	private boolean decorationsDone = false;
	private boolean showSyslog = false;

	public Worker(Integer index, Login profile, SocksProxy socks, Integer lastTutorial, String workerName, Boolean needTuturialsAccept) {
		this.index = index;
		this.profile = profile;
		this.socks = socks;
		if(lastTutorial != null) {
			this.lastTutorial = lastTutorial.intValue();
		} else if(needTuturialsAccept != null && needTuturialsAccept.booleanValue()) {
			this.lastTutorial = 3400000;
		} else {
			this.lastTutorial = 3500000;
		}

		this.name = workerName;
		this.startAccountWorker();
	}

	public void startAccountWorker() {
		this.ap = new AccountProcess(this.name, this.profile, this.socks, this);
		this.ap.setDetailedLog(this.showSyslog);
		this.ap.start();
		this.startTime = new Date();
	}

	public void stop() {
		if(this.ap.isStarted()) {
			this.ap.disconnect();
		}

	}

	public void setClaimRssSlotMode() {
		this.claimRssSlots = true;
	}

	public void setCompleteTutorialsMode() {
		this.completedTutorials = 0;
		this.completeTutorials = true;
	}

	public boolean getDone() {
		return this.done;
	}

	public Integer getLastTutorial() {
		return Integer.valueOf(this.lastTutorial);
	}

	private int execFixBuildings(EndClientTurn ectm, int lastTick, int cmdcount) {
		int uses = 0;

		int i;
		for(i = 0; i < 6; ++i) {
			ectm.addLogicCommand(new FixBuilding(lastTick, 1000000, i));
			lastTick += 2;
			++uses;
		}

		for(i = 0; i < 6; ++i) {
			ectm.addLogicCommand(new FixBuilding(lastTick, 1400000, i));
			lastTick += 2;
			++uses;
		}

		for(i = 0; i < 4; ++i) {
			ectm.addLogicCommand(new FixBuilding(lastTick, 1800000, i));
			lastTick += 2;
			++uses;
		}

		return uses;
	}

	private int tutorialFastActions(EndClientTurn ectm, int lastTick) {
		int cmdcount = 0;
		int uses;
		AnimalsProduction chicken;
		Field wheat;
		int var10000;
		switch(this.lastTutorial) {
			case 3400000:
				if(this.completedTutorials < 40) {
					for(int var18 = this.completedTutorials; var18 < this.completedTutorials + 10; ++var18) {
						ectm.addLogicCommand(new StartTutorial(lastTick, GlobalID.create(36, var18)));
						lastTick += 2;
						ectm.addLogicCommand(new TutorialCompleted(lastTick, GlobalID.create(36, var18)));
						lastTick += 2;
					}

					this.completedTutorials += 10;
					this.syslog(Logger.formatPT(this.name, "  Accept " + this.completedTutorials + " tutorials"));
				}
				break;
			case 3500000:
				chicken = this.farm.getAnimalProduction("Chicken");
				AnimalEntry ae = Animals.getByName("Chicken");
				if(ae != null) {
					this.syslog(Logger.formatPT(this.name, "  Claim The Chickens Habitat. Buy 6 Chickens."));
					ectm.addLogicCommand(new CollectCompletedBuildingExp(lastTick, GlobalID.create(21, 0)));
					lastTick += 2;
					ectm.addLogicCommand(new BuyAnimal(lastTick, ae.getGlobalID().intValue(), GlobalID.create(11, 0), 7369, 10912, false));
					lastTick += 2;
					ectm.addLogicCommand(new BuyAnimal(lastTick, ae.getGlobalID().intValue(), GlobalID.create(11, 0), 7567, 11497, false));
					lastTick += 2;
					ectm.addLogicCommand(new BuyAnimal(lastTick, ae.getGlobalID().intValue(), GlobalID.create(11, 0), 7175, 11748, false));
					lastTick += 2;
					ectm.addLogicCommand(new BuyAnimal(lastTick, ae.getGlobalID().intValue(), GlobalID.create(11, 0), 7372, 10912, false));
					lastTick += 2;
					ectm.addLogicCommand(new BuyAnimal(lastTick, ae.getGlobalID().intValue(), GlobalID.create(11, 0), 7570, 11497, false));
					lastTick += 2;
					ectm.addLogicCommand(new BuyAnimal(lastTick, ae.getGlobalID().intValue(), GlobalID.create(11, 0), 7178, 11748, false));
					lastTick += 2;
					cmdcount += 7;

					for(int var19 = 0; var19 < 6; ++var19) {
						chicken.add(new DisplayProductionObject(ae.getGlobalID().intValue(), 1, this.farm.getNextAnimalInstance()));
					}

					this.farm.readyToCompleteBuildingExp().clear();
					this.lastTutorial = 3500001;
				}
				break;
			case 3500001:
				this.syslog(Logger.formatPT(this.name, "  Feed 6 Chickens. Speedup 6 chikens. Collect 3 Eggs. Feed 3 Chickens"));
				chicken = this.farm.getAnimalProduction("Chicken");
				uses = this.startProductions(chicken, ectm, lastTick, cmdcount);
				lastTick += uses * 8;
				cmdcount += uses;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 0)));
				lastTick += 2;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 1)));
				lastTick += 2;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 2)));
				lastTick += 2;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 3)));
				lastTick += 2;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 4)));
				lastTick += 2;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 5)));
				lastTick += 8;
				cmdcount += 6;
				chicken.speedup(lastTick);
				uses = this.collectProductions(chicken, ectm, lastTick, cmdcount, 3);
				lastTick += uses * 8;
				cmdcount += uses;
				uses = this.startProductions(chicken, ectm, lastTick, cmdcount);
				lastTick += uses * 8;
				cmdcount += uses;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 0)));
				lastTick += 2;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 1)));
				lastTick += 2;
				ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 2)));
				lastTick += 2;
				chicken.speedup(lastTick);
				this.lastTutorial = 3500002;
				break;
			case 3500002:
			case 3500003:
				this.syslog(Logger.formatPT(this.name, "  Harvest 6 wheats, Plant 6 wheats, SpeedUp 6 wheats"));
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				wheat = new Field(GlobalID.create(4, 1));
				uses = this.farm.sowSeeds(wheat.id(), 6, wheat.timeSec(), ectm, lastTick, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				uses = this.farm.speedupCrops(ectm, lastTick, this.showSyslog);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				++this.lastTutorial;
				break;
			case 3500004:
				this.syslog(Logger.formatPT(this.name, "  Harvest 6 wheats, Plant 3 wheats, SpeedUp 3 wheats, Harvest 3 wheats"));
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				wheat = new Field(GlobalID.create(4, 1));
				uses = this.farm.sowSeeds(wheat.id(), 3, wheat.timeSec(), ectm, lastTick, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				uses = this.farm.speedupCrops(ectm, lastTick, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				this.lastTutorial = 3500005;
				break;
			case 3500005:
				this.syslog(Logger.formatPT(this.name, "  Plant 4 corns, SpeedUp 4 corns, Harvest 4 corns, Plant 5 corns, SpeedUp 5 corns"));
				Field corn = new Field(GlobalID.create(4, 2));
				uses = this.farm.sowSeeds(corn.id(), 4, corn.timeSec(), ectm, lastTick, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				uses = this.farm.speedupCrops(ectm, lastTick, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, 3, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				uses = this.farm.sowSeeds(corn.id(), 5, corn.timeSec(), ectm, lastTick, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				uses = this.farm.speedupCrops(ectm, lastTick, this.showSyslog);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				this.lastTutorial = 3500006;
				break;
			case 3500006:
				this.syslog(Logger.formatPT(this.name, "  Buy Bakery, SpeedUp Bakery Construction, Bake 2 breads, SpeedUp 2 bread, Bake 2 bread."));
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(6, 0), 19, 15, true));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new SpeedUpBuildingConstruction(lastTick, GlobalID.create(21, 1)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new CollectCompletedBuildingExp(lastTick, GlobalID.create(21, 1)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new StartProduction(lastTick, GlobalID.create(6, 0), GlobalID.create(24, 0)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new StartProduction(lastTick, GlobalID.create(6, 0), GlobalID.create(24, 0)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new SpeedUpProduction(lastTick, GlobalID.create(6, 0)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new SpeedUpProduction(lastTick, GlobalID.create(6, 0)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new StartProduction(lastTick, GlobalID.create(6, 0), GlobalID.create(24, 0)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new StartProduction(lastTick, GlobalID.create(6, 0), GlobalID.create(24, 0)));
				lastTick += 8;
				++cmdcount;
				this.farm.updateCurrentExp(Integer.valueOf(9));
				this.lastTutorial = 3500007;
				break;
			case 3500007:
				this.syslog(Logger.formatPT(this.name, "  Buy FeedMill, Place Fields, Plant Crops"));
				FactoryEntry feedmill = ProcessingBuildings.getByName("Hammermill");
				ectm.addLogicCommand(new BuyObject(lastTick, feedmill.getId(), 22, 15, true));
				int ticksLeft = Utils.secondsToObjectTicks(feedmill.getConstructionTime());
				DisplayObjectTimer t = new DisplayObjectTimer(feedmill.getConstructionTime(), ticksLeft, lastTick);
				lastTick += 2;
				++cmdcount;
				ConstructionObject co = new ConstructionObject(Integer.valueOf(feedmill.getId()), t, Integer.valueOf(1));
				this.farm.constructionBuildings().put(Integer.valueOf(feedmill.getId()), co);
				this.farm.getNextConstructionID();
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 21, 20, false));
				lastTick += 2;
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 21, 21, false));
				lastTick += 2;
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 21, 22, false));
				lastTick += 2;

				for(int var20 = 0; var20 < 3; ++var20) {
					int var21 = this.farm.getFields().size();
					DisplayObject var22 = new DisplayObject(GlobalID.create(4, 0), 400000 + var21);
					var22.setField(this.farm.getNextFieldID());
					this.farm.getFields().add(var21, var22);
				}

				cmdcount += 3;
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				wheat = new Field(GlobalID.create(4, 1));
				uses = this.farm.sowSeeds(wheat.id(), 9, wheat.timeSec(), ectm, lastTick, this.showSyslog);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				this.lastTutorial = 3500008;
				break;
			case 3500008:
				this.syslog(Logger.formatPT(this.name, "  Fix Buildings."));
				uses = this.execFixBuildings(ectm, lastTick, cmdcount);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				this.farm.updateCurrentExp(Integer.valueOf(16));
				this.ap.putMessage(new ChangeFarmName(this.name));
				this.lastTutorial = 3500009;
				break;
			case 3500009:
				this.syslog(Logger.formatPT(this.name, "  Waiting for Feed Mill."));
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				wheat = new Field(GlobalID.create(4, 1));
				uses = this.farm.sowSeeds(wheat.id(), 6, wheat.timeSec(), ectm, lastTick, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				if(this.farm.readyToCompleteBuildingExp().size() > 0) {
					Iterator fId = this.farm.readyToCompleteBuildingExp().keySet().iterator();

					while(fId.hasNext()) {
						Integer buildingId = (Integer)fId.next();
						ectm.addLogicCommand(new CollectCompletedBuildingExp(lastTick, buildingId.intValue()));
						++cmdcount;
						lastTick += 8;
						Integer oId = (Integer)this.farm.readyToCompleteBuildingExp().get(buildingId);
						ProductionFactories pcfg = this.farm.getProductionById(oId);
						FactoryEntry fe = ProcessingBuildings.getById(oId.intValue());
						if(pcfg != null && fe != null) {
							pcfg.add(new DisplayProductionObject(oId.intValue(), fe.getSlots(), 1));
							this.syslog(Logger.formatPT(this.name, "  " + fe.getName() + " build completed"));
						}
					}

					this.lastTutorial = 3500010;
					this.farm.readyToCompleteBuildingExp().clear();
					this.syslog(Logger.formatPT(this.name, "  Make Chicken Food. SpeedUp Chicken Food. Feed 3 Chickens. SpeedUp 3 Chickens. Collect 3 Eggs."));
					ectm.addLogicCommand(new StartProduction(lastTick, GlobalID.create(6, 1), GlobalID.create(16, 0)));
					lastTick += 8;
					++cmdcount;
					ectm.addLogicCommand(new SpeedUpProduction(lastTick, GlobalID.create(6, 1)));
					lastTick += 8;
					++cmdcount;
					this.farm.getInventory().put(Integer.valueOf(GlobalID.create(16, 0)), Integer.valueOf(3));
					chicken = this.farm.getAnimalProduction("Chicken");
					uses = this.startProductions(chicken, ectm, lastTick, 27);
					lastTick += uses * 8;
					cmdcount += uses;
					ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 0)));
					lastTick += 2;
					ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 1)));
					lastTick += 2;
					ectm.addLogicCommand(new SpeedUp(lastTick, GlobalID.create(13, 2)));
					lastTick += 2;
					cmdcount += 3;
					chicken.speedup(lastTick);
					uses = this.collectProductions(chicken, ectm, lastTick, cmdcount, 3);
					var10000 = lastTick + uses * 8;
					cmdcount += uses;
				} else {
					uses = this.collectProductions(this.farm.getBakery(), ectm, lastTick, cmdcount);
					lastTick += uses * 8;
					cmdcount += uses;
					uses = this.collectProductions(this.farm.getAnimalProduction("Chicken"), ectm, lastTick, cmdcount);
					var10000 = lastTick + uses * 8;
					cmdcount += uses;
				}
				break;
			case 3500010:
				this.syslog(Logger.formatPT(this.name, "  empty step."));
				this.lastTutorial = 3500012;
				break;
			case 3500011:
				this.syslog(Logger.formatPT(this.name, "  Waiting to ship done."));
				uses = this.collectProductions(this.farm.getBakery(), ectm, lastTick, cmdcount);
				lastTick += uses * 8;
				cmdcount += uses;
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				wheat = new Field(GlobalID.create(4, 1));
				uses = this.farm.sowSeeds(wheat.id(), 6, wheat.timeSec(), ectm, lastTick, this.showSyslog);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				if(--this.iterations <= 0) {
					this.lastTutorial = 3500013;
				}
				break;
			case 3500012:
				this.syslog(Logger.formatPT(this.name, "  Shipping first order..."));
				ectm.addLogicCommand(new Shipment(lastTick, 0));
				lastTick += 8;
				++cmdcount;
				this.lastTutorial = 3500011;
				this.iterations = 3;
				break;
			case 3500013:
				this.syslog(Logger.formatPT(this.name, "  Ship first order done, Collect coins, Shipping second order..."));
				ectm.addLogicCommand(new ClaimShipment(lastTick));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new GetOrder(lastTick, GlobalID.create(32, 0)));
				lastTick += 8;
				++cmdcount;
				ectm.addLogicCommand(new Shipment(lastTick, 0));
				lastTick += 8;
				++cmdcount;
				this.lastTutorial = 3500014;
				this.iterations = 3;
				break;
			case 3500014:
				this.syslog(Logger.formatPT(this.name, "  Waiting to ship done."));
				uses = this.collectProductions(this.farm.getBakery(), ectm, lastTick, cmdcount);
				lastTick += uses * 8;
				cmdcount += uses;
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				wheat = new Field(GlobalID.create(4, 1));
				uses = this.farm.sowSeeds(wheat.id(), 9, wheat.timeSec(), ectm, lastTick, this.showSyslog);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				if(--this.iterations <= 0) {
					this.lastTutorial = 3500015;
				}
				break;
			case 3500015:
				this.syslog(Logger.formatPT(this.name, "  Ship second order done."));
				uses = this.collectProductions(this.farm.getBakery(), ectm, lastTick, cmdcount);
				lastTick += uses * 8;
				cmdcount += uses;
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				ectm.addLogicCommand(new ClaimShipment(lastTick));
				lastTick += 8;
				++cmdcount;
				this.lastTutorial = 3500100;
				break;
			case 3500100:
				this.syslog(Logger.formatPT(this.name, "  initial level up complete..."));
				uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
				lastTick += uses * 2;
				cmdcount += uses;
				wheat = new Field(GlobalID.create(4, 1));
				uses = this.farm.sowSeeds(wheat.id(), 6, wheat.timeSec(), ectm, lastTick, this.showSyslog);
				var10000 = lastTick + uses * 2;
				cmdcount += uses;
				this.lastTutorial = 3500101;
		}

		return cmdcount;
	}

	private void constructionActions(int ticksNow) {
		ArrayList coready = new ArrayList();
		Iterator var4 = this.farm.constructionBuildings().values().iterator();

		ConstructionObject cor;
		while(var4.hasNext()) {
			cor = (ConstructionObject)var4.next();
			if(cor.getRmainingSeconds(ticksNow) <= 0) {
				coready.add(cor);
			}
		}

		var4 = coready.iterator();

		while(var4.hasNext()) {
			cor = (ConstructionObject)var4.next();
			this.farm.readyToCompleteBuildingExp().put(Integer.valueOf(this.farm.getNextConstructionID()), cor.getTargetId());
			this.farm.constructionBuildings().remove(cor.getTargetId());
		}

	}

	private int checkFieldsCount(EndClientTurn ectm, int lastTick) {
		int cmdcount = 0;
		if(this.farm.getCurrentLevel().intValue() < 7) {
			int mf = ExpLevels.getMaxFields(this.farm.getCurrentLevel().intValue()).intValue();
			int i;
			int fId;
			DisplayObject hddo;
			if(mf == 9 && this.farm.getFields().size() < mf && this.f9trycount == 0) {
				++this.f9trycount;
				this.syslog(Logger.formatPT(this.name, "  Buy 3 Fields."));
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 21, 20, false));
				lastTick += 2;
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 21, 21, false));
				lastTick += 2;
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 21, 22, false));
				lastTick += 2;
				cmdcount += 3;

				for(i = 0; i < 3; ++i) {
					fId = this.farm.getFields().size();
					hddo = new DisplayObject(GlobalID.create(4, 0), 400000 + fId);
					hddo.setField(this.farm.getNextFieldID());
					this.farm.getFields().add(fId, hddo);
				}
			}

			if(mf == 12 && this.farm.getFields().size() < mf && this.f12trycount == 0) {
				++this.f12trycount;
				this.syslog(Logger.formatPT(this.name, "  Buy 3 Fields."));
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 21, 23, false));
				lastTick += 2;
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 20, 23, false));
				lastTick += 2;
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(4, 0), 19, 23, false));
				lastTick += 2;
				cmdcount += 3;

				for(i = 0; i < 3; ++i) {
					fId = this.farm.getFields().size();
					hddo = new DisplayObject(GlobalID.create(4, 0), 400000 + fId);
					hddo.setField(this.farm.getNextFieldID());
					this.farm.getFields().add(fId, hddo);
				}
			}
		}

		return cmdcount;
	}

	private int mysteryBoxSlot(EndClientTurn ectm, int lastTick) {
		int cmdcount = 0;
		int diamonds;
		Integer curd;
		if(this.farm.getOpenBox() != null && this.farm.getOpenBox().state() == MysteryBoxState.Unlocked) {
			ectm.addLogicCommand(new OpenMysteryBox(lastTick));
			++cmdcount;
			lastTick += 2;
			ectm.addLogicCommand(new FadeAwayMysteryBox(lastTick));
			++cmdcount;
			lastTick += 2;
			this.syslog(Logger.formatPT(this.name, "  Mystery Box: GoodGlobalId = \"" + StaticData.getGlobalName(Integer.valueOf(this.farm.getOpenBox().GoodGlobalId())) + "\", GoodAmount = " + this.farm.getOpenBox().GoodAmount() + " collected"));
			diamonds = 0;
			curd = (Integer)this.farm.getInventory().get(Integer.valueOf(this.farm.getOpenBox().GoodGlobalId()));
			if(curd != null) {
				diamonds = curd.intValue();
			}

			diamonds += this.farm.getOpenBox().GoodAmount();
			this.farm.getInventory().put(Integer.valueOf(this.farm.getOpenBox().GoodGlobalId()), Integer.valueOf(diamonds));
			this.farm.clearOpenBox();
		}

		if(this.farm.getOpenBox() != null && this.farm.getOpenBox().state() == MysteryBoxState.Locked) {
			diamonds = 0;
			curd = (Integer)this.farm.getInventory().get(Integer.valueOf(1900001));
			if(curd != null) {
				diamonds = curd.intValue();
			}

			if(IDs.isDiamonds(Integer.valueOf(this.farm.getOpenBox().GoodGlobalId())) && diamonds >= this.farm.getOpenBox().UnlockPrice()) {
				ectm.addLogicCommand(new OpenMysteryBox(lastTick));
				++cmdcount;
				lastTick += 2;
				ectm.addLogicCommand(new FadeAwayMysteryBox(lastTick));
				++cmdcount;
				lastTick += 2;
				this.syslog(Logger.formatPT(this.name, "  Mystery Box Locked: GoodGlobalId = " + this.farm.getOpenBox().GoodGlobalId() + ", GoodAmoiunt = " + this.farm.getOpenBox().GoodAmount() + " collected"));
				int goodcount = 0;
				Integer gci = (Integer)this.farm.getInventory().get(Integer.valueOf(this.farm.getOpenBox().GoodGlobalId()));
				if(gci != null) {
					goodcount = gci.intValue();
				}

				goodcount += this.farm.getOpenBox().GoodAmount();
				this.farm.getInventory().put(Integer.valueOf(this.farm.getOpenBox().GoodGlobalId()), Integer.valueOf(goodcount));
			} else {
				ectm.addLogicCommand(new CancelMysteryBox(lastTick));
				++cmdcount;
				lastTick += 2;
				this.syslog(Logger.formatPT(this.name, "  Mystery Box Locked: GoodGlobalId = " + this.farm.getOpenBox().GoodGlobalId() + ", GoodAmoiunt = " + this.farm.getOpenBox().GoodAmount() + " canceled"));
			}

			this.farm.clearOpenBox();
		}

		return cmdcount;
	}

	private int orderSlot(EndClientTurn ectm, int lastTick, boolean force) {
		int cmdcount = 0;
		if(this.nextOrderTimer == null) {
			this.nextOrderTimer = new LogicTimer();
			this.nextOrderTimer.startTimer(Utils.secondsToObjectTicks(this.orderToOrderTime), lastTick);
		}

		OrderEntry oe = this.farm.getOrders().getAcceptedOrder(this.farm.getInventory());
		if(oe != null) {
			if(oe.getState() == state.needToCancel) {
				this.syslog(Logger.formatPT(this.name, "  " + oe + " canceled."));
				ectm.addLogicCommand(new CancelShipment(lastTick, oe.index().intValue()));
				this.farm.getOrders().addToShipped(oe);
				this.farm.getOrders().clearShipped();
				++cmdcount;
				lastTick += 2;
			} else if(force || this.nextOrderTimer.getRemainingSeconds(lastTick) <= 0) {
				if(oe.getState() == state.readyToShip) {
					this.syslog(Logger.formatPT(this.name, "  " + oe + " ready for shipment."));
					ectm.addLogicCommand(new Shipment(lastTick, oe.index().intValue()));
					++cmdcount;
					lastTick += 2;
					Iterator var7 = oe.items().keySet().iterator();

					while(var7.hasNext()) {
						Integer id = (Integer)var7.next();
						Integer count = (Integer)oe.items().get(id);
						Integer present = (Integer)this.farm.getInventory().get(id);
						this.farm.getInventory().put(id, Integer.valueOf(present.intValue() - count.intValue()));
					}

					this.farm.getOrders().addToShipped(oe);
				}

				if(oe.getState() == state.Shipped) {
					this.syslog(Logger.formatPT(this.name, "  " + oe + " shipped."));
					ectm.addLogicCommand(new ClaimShipment(lastTick));
					++cmdcount;
					lastTick += 2;
					this.farm.getOrders().clearShipped();
					this.farm.updateCurrentExp(oe.exp());
					this.nextOrderTimer.startTimer(Utils.secondsToObjectTicks(this.orderToOrderTime), lastTick);
				}
			}
		}

		return cmdcount;
	}

	private int collectProductions(ProductionFactories f, EndClientTurn ectm, int lastTick, int cmdcount) {
		return this.collectProductions(f, ectm, lastTick, cmdcount, 10000);
	}

	private int collectProductions(ProductionFactories f, EndClientTurn ectm, int lastTick, int cmdcount, int maxcount) {
		int uses = 0;
		int barnEmpty = this.farm.getBarnEmpty();
		int completed = 0;

		int readySize;
		while(completed < maxcount && (readySize = f.getReadyCount()) > 0 && cmdcount < 30 && barnEmpty > readySize * f.getUnitsMultiplier()) {
			if(cmdcount + readySize > 30) {
				readySize = 30 - cmdcount;
			}

			if(readySize > maxcount) {
				readySize = maxcount;
			}

			LinkedHashMap ready = f.claimReady(readySize);
			if(ready.size() > 0) {
				Iterator var12 = ready.keySet().iterator();

				while(var12.hasNext()) {
					Integer smelterId = (Integer)var12.next();
					String ids = new String();
					Iterator var15 = ((List)ready.get(smelterId)).iterator();

					while(var15.hasNext()) {
						Integer i = (Integer)var15.next();
						ids = ids + "\"" + f.getGoodById(i).getName() + "\"" + " ";
						Integer ci = (Integer)this.farm.getInventory().get(i);
						if(ci == null) {
							ci = Integer.valueOf(0);
						}

						this.farm.getInventory().put(i, Integer.valueOf(ci.intValue() + f.getUnitsMultiplier()));
						if(f instanceof AnimalsProduction) {
							ectm.addLogicCommand(new CollectAnimalGoods(lastTick, smelterId.intValue()));
						} else {
							ectm.addLogicCommand(new CollectProductions(lastTick, smelterId.intValue()));
						}

						lastTick += 8;
						++cmdcount;
						++uses;
						++completed;
						this.farm.updateCurrentExp(f.getGoodById(i).getExpCollect());
					}

					if(f.logEnable()) {
						this.syslog(Logger.formatPT(this.name, "  " + f.getProductionName() + "( " + smelterId + " ): Ready:[ " + ids + "]"));
					}
				}
			}
		}

		return uses;
	}

	private int startProductions(ProductionFactories f, EndClientTurn ectm, int lastTick, int cmdcount) {
		int uses = 0;
		Integer maxEmptyId = null;
		if(f.onlyOneDpoPerSlot()) {
			maxEmptyId = f.getMaxEmptyIdentifier();
		}

		while(cmdcount < 30) {
			int emptyCount = 0;
			if(f.onlyOneDpoPerSlot()) {
				if(maxEmptyId != null) {
					emptyCount = f.getEmptyCount(maxEmptyId);
				}
			} else {
				maxEmptyId = null;
				emptyCount = f.getEmptyCount();
			}

			if(emptyCount <= 0) {
				break;
			}

			String hqueue = f.headQueue();
			if(hqueue == null) {
				this.syslog(Logger.formatPT(this.name, "  " + f.getProductionName() + ": headQueue == null"));
				break;
			}

			ProductionsGoodEntry Good = f.getGoodByName(hqueue);
			String rstr = new String();
			if(Good == null) {
				this.syslog(Logger.formatPT(this.name, "  " + f.getProductionName() + ": Good == NULL"));
				break;
			}

			String fieldsLim;
			for(Iterator needed = Good.getRequirements().keySet().iterator(); needed.hasNext(); rstr = rstr + fieldsLim + "(" + Good.getRequirements().get(fieldsLim) + ") ") {
				fieldsLim = (String)needed.next();
			}

			if(f.logEnable()) {
				this.syslog(Logger.formatPT(this.name, "  " + f.getProductionName() + ": produce queue:" + f.queueToString()));
				this.syslog(Logger.formatPT(this.name, "  " + f.getProductionName() + ": Try to produce \"" + Good.getName() + "\", Required:[ " + rstr + "]"));
			}

			int var19 = this.getSiloSellLimits() / 2;
			if(var19 < 2) {
				var19 = 2;
			}

			LinkedHashMap var20 = f.neededToProduce(f.headQueue(), this.farm.getInventory(), this.farm.getCurrentLevel().intValue(), Integer.valueOf(var19));
			String goodMaterial;
			Iterator var15;
			if(var20 != null) {
				if(f.logEnable()) {
					String var21 = new String();

					for(var15 = var20.keySet().iterator(); var15.hasNext(); var21 = var21 + goodMaterial + "(" + var20.get(goodMaterial) + ") ") {
						goodMaterial = (String)var15.next();
					}

					this.syslog(Logger.formatPT(this.name, "  " + f.getProductionName() + ": \"" + f.headQueue() + "\": missing components:[ " + var21 + "]"));
				}

				f.shiftQueue();
				break;
			}

			f.shiftQueue();
			Integer productionId = null;
			if(maxEmptyId != null) {
				productionId = f.produce(maxEmptyId, Good.getGlobalID(Integer.valueOf(f.getObjectClass())), lastTick);
			} else {
				productionId = f.produce(Good.getGlobalID(Integer.valueOf(f.getObjectClass())).intValue(), lastTick);
			}

			if(productionId != null) {
				var15 = Good.getRequirements().keySet().iterator();

				while(var15.hasNext()) {
					goodMaterial = (String)var15.next();
					int reqId = f.componentIdByName(goodMaterial).intValue();
					int reqAmount = ((Integer)Good.getRequirements().get(goodMaterial)).intValue();
					Integer curAmount = (Integer)this.farm.getInventory().get(Integer.valueOf(reqId));
					this.farm.getInventory().put(Integer.valueOf(reqId), Integer.valueOf(curAmount.intValue() - reqAmount));
				}

				if(f.logEnable()) {
					this.syslog(Logger.formatPT(this.name, "  " + f.getProductionName() + "( " + productionId + " ): Put \"" + Good.getName() + "\""));
				}

				if(f instanceof AnimalsProduction) {
					ectm.addLogicCommand(new StartAnimalGoods(lastTick, productionId.intValue()));
				} else {
					ectm.addLogicCommand(new StartProduction(lastTick, productionId.intValue(), Good.getGlobalID(Integer.valueOf(f.getObjectClass())).intValue()));
				}

				lastTick += 8;
				++cmdcount;
				++uses;
			}
		}

		return uses;
	}

	private int getSiloSellLimits() {
		return 2;
	}

	public void ownHomeDataEvent(Long AccountID, Long HomeID, OwnHomeData home) {
		this.lastReload = new Date();
		int groupIndex = AccountID.intValue() % LogicEventData.getEvent(0).getGroupSize().intValue();
		String sevt = new String();

		for(int i = 0; i < 11; ++i) {
			LogicEventDataEntry oes = LogicEventData.getEvent(i);
			Integer weight = oes.getByGroup(groupIndex);
			sevt = sevt + "  \"" + oes.getGoodReward() + "\": " + weight + " ";
		}

		this.syslog(Logger.formatPT(this.name, "  EventGroupIndex: " + groupIndex + " { " + sevt + "}"));
		if(this.farm == null) {
			this.farm = new Farm(home, this.name);
		} else {
			this.farm.update(home);
		}

		this.farm.reload(!this.showSyslog);
		Iterator var9 = this.farm.getOrders().iterator();

		while(var9.hasNext()) {
			String var10 = ((OrderEntry)var9.next()).toString();
			if(var10 != null) {
				this.syslog(Logger.formatPT(this.name, "  " + var10));
			}
		}

	}

	public void ownCityDataEvent(Long AccountID, Long HomeID, OwnCityData city) {
	}

	public void ownFishingDataEvent(Long AccountID, Long HomeID, OwnFishingData fishing) {
	}

	public void visitedHomeDataEvent(Long AccountID, Long HomeID, VisitedHomeData visited) {
	}

	public Object tickActionEvent(Long AccountID, Long HomeID, EndClientTurn ectm, int beginTick, int endTick) {
		int lastTick = beginTick + 3 & -2;
		if(this.claimRssSlots) {
			return this.farm.rssClaimSlot(ectm, lastTick, this.showSyslog) > 0?null:new AccountStopException((Base)null, "Worker Done!");
		} else if(this.completeTutorials) {
			this.lastTutorial = 3400000;
			if(this.completedTutorials < 40) {
				this.tutorialFastActions(ectm, lastTick);
				return null;
			} else {
				return new AccountStopException((Base)null, "Worker Done!");
			}
		} else if(!this.done && this.farm.getCurrentLevel().intValue() >= 7) {
			System.out.println(Logger.formatPT(this.name, "  Worker Done: " + ((new Date()).getTime() - this.startTime.getTime()) / 1000L + " seconds."));
			this.done = true;
			return null;
		} else if(this.done) {
			return new AccountStopException((Base)null, "Worker Done!");
		} else if(this.farm.getOpenBox() != null && this.farm.getOpenBox().state() == MysteryBoxState.NotChecked) {
			ectm.addLogicCommand(new CheckMysteryBoxLocked(lastTick));
			return null;
		} else if(this.mysteryBoxSlot(ectm, lastTick) > 0) {
			return null;
		} else if(this.farm.getCurrentLevel().intValue() == 6 && this.farm.getCurrentExp().intValue() >= 110 && this.decorationsDone) {
			if(this.farm.getHarvestCount().intValue() >= 50) {
				this.syslog(Logger.formatPT(this.name, "  Claim achivements."));
				ectm.addLogicCommand(new ClaimAchivement(lastTick, GlobalID.create(59, 0)));
				lastTick += 60;
				ectm.addLogicCommand(new ClaimAchivement(lastTick, GlobalID.create(59, 12)));
				lastTick += 60;
				ectm.addLogicCommand(new ClaimAchivement(lastTick, GlobalID.create(59, 25)));
				this.farm.updateCurrentExp(Integer.valueOf(110));
			} else {
				this.syslog(Logger.formatPT(this.name, "  Waiting for harvest 50 fields... ( " + (50 - this.farm.getHarvestCount().intValue()) + " )"));
				this.farm.collectHarvest(ectm, lastTick, 0, this.showSyslog);
			}

			return null;
		} else if(this.farm.getCurrentLevel().intValue() == 6 && this.farm.getCurrentExp().intValue() >= 60) {
			if(this.finallyCheckedMysteryBox) {
				this.syslog(Logger.formatPT(this.name, "  Buy 2 caterpillars."));
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(3, 42), 21, 31, false));
				lastTick += 2;
				ectm.addLogicCommand(new BuyObject(lastTick, GlobalID.create(3, 42), 22, 31, false));
				this.farm.updateCurrentExp(Integer.valueOf(50));
				this.decorationsDone = true;
			} else {
				this.lastTutorial = 0;
				this.finallyCheckedMysteryBox = true;
				this.syslog(Logger.formatPT(this.name, "  Finally check for mysterybox."));
			}

			return null;
		} else {
			this.constructionActions(lastTick);
			if(this.lastTutorial > 0) {
				if(this.lastTutorial < 3500101) {
					this.tutorialFastActions(ectm, lastTick);
					if((this.lastTutorial == 0 || this.lastTutorial == 3500012) && this.lastTutorial == 3500012) {
						return new AccountStopAndReconnectException((Base)null, "", 3000);
					}
				} else {
					if(this.checkFieldsCount(ectm, lastTick) > 0) {
						return null;
					}

					if(this.orderSlot(ectm, lastTick, false) > 0) {
						return null;
					}

					if(this.farm.forestSlot(ectm, lastTick, this.showSyslog) > 0) {
						return null;
					}

					byte cmdcount = 0;
					int uses = this.collectProductions(this.farm.getBakery(), ectm, lastTick, cmdcount);
					lastTick += uses * 8;
					int cmdcount1 = cmdcount + uses;
					uses = this.farm.collectHarvest(ectm, lastTick, cmdcount1, this.showSyslog);
					lastTick += uses * 2;
					cmdcount1 += uses;
					Field wheat = new Field(GlobalID.create(4, 1));
					uses = this.farm.sowSeeds(wheat.id(), 100, wheat.timeSec(), ectm, lastTick, this.showSyslog);
					int var10000 = lastTick + uses * 2;
					var10000 = cmdcount1 + uses;
				}
			}

			return null;
		}
	}

	public void endClientTurnOkEvent(Long AccountID, Long HomeID, EndClientTurnOK msg) {
		Long timediff = Long.valueOf((new Date()).getTime() - this.lastReload.getTime());
		if(this.lastTutorial == 3500101 && timediff.longValue() >= 30000L) {
			if(this.nextOrderTimer != null) {
				this.nextOrderTimer.startTimer(Utils.secondsToObjectTicks(this.nextOrderTimer.getRemainingSeconds(msg.getTLast().intValue())), 0);
			}

			this.ap.putMessage(new GoHome());
		}

		if(this.lastTutorial == 0) {
			this.lastTutorial = 3500102;
			this.ap.putMessage(new GoHome());
		}

	}

	public void messageSentEvent(Long AccountID, Long HomeID, Object anydata) {
		if(this.farm.getOpenBox() != null && this.farm.getOpenBox().state() == MysteryBoxState.NotChecked) {
			this.ap.putMessage(new GoHome());
		}

	}

	public void messageReceiveEvent(Long AccountID, Long HomeID, Base msg) {
	}

	public void unknownMessageReceiveEvent(Long AccountID, Long HomeID, Unknown msg) {
	}

	public Base loddegInEvent(Long AccountID, Long HomeID, LoginOK lOk) {
		this.syslog(Logger.formatPT(this.name, "  worker started."));
		return new GoHome();
	}

	public void stoppedEvent(Long AccountID, Long HomeID) {
		this.syslog(Logger.formatPT(this.name, "  worker stopped."));
	}

	public void updateLoginData(Long AccountID, Long HomeID, String loginData) {
		this.syslog("Update Login Data:");
		this.syslog(loginData);
		Login nld = new Login(StringUtils.stringToByteArray(loginData));
		nld.HomeID = HomeID;
		FarmsWork.changeAccount(this.index.intValue(), nld);
	}

	private void syslog(String log) {
		if(this.showSyslog) {
			System.out.println(log);
		}

	}
}
